/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package  net.babeuf.form.samples
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import net.babeuf.form.errors.FieldError;
	import net.babeuf.form.event.FieldErrorEvent;
	import net.babeuf.form.Form;
	
	/**
	 * First sample of AS3 Form use
	 * @author Damien Pegoraro
	 */public class MetaSample extends Sprite 
	{
		[Embed(source='assets/formdemo.swf', symbol='formSample1')]
		private static var FORM_VIEW : Class ; 
				
		private var view : Sprite  ;
		
		
		public var form : Form ; 
						
		[Field(type = "InputField", order = 1, id = "name", defaultLabel = "Type your name", optional = true)]
		[Sensor(type="TextFieldColorSensor", color=0xFF0000)]
		public var firstname : TextField  ; 
		
		[Field(type = "EmailField", order=2, id="email", defaultLabel = "Type your email", emptyError = "email_empty", invalidError = "email_invalid")]
		[Sensor(type="TextFieldColorSensor", color=0xFF0000)]
		public var email : TextField  ;
		
		[Field(type = "PasswordField", order=3, id = "password", defaultLabel = "Type your password", emptyError = "password_empty", minError = "password_too_small")]
		[Sensor(type = "TextFieldColorSensor", color = 0xFF0000)]
		[Sensor(type = "TextFieldColorSensor", condition = "match", color = 0xFF0000)]
		public var password : TextField  ;
		
		[Field(type = "PasswordField", order=4, notSent = true, id="confirm", defaultLabel = "Confirm your password", emptyError = "password_confirm_empty", minError = "password_confirm_too_small")]
		[Sensor(type = "TextFieldColorSensor", id="confirm", color = 0xFF0000)]		
		[Sensor(type = "TextFieldColorSensor", condition = "match", color = 0xFF0000)]
		public var passwordConfirm : TextField  ;
				
		[Condition(type = "MatchCondition", id="match", field1 = "password", field2 = "confirm", error = "password_confirm_invalid")]			
		public var matchCondition : String ; 
		
				
		
		public function MetaSample() 
		{			
			buildView() ; 	
			buildForm() ; 	
		}
		
		private function buildForm():void 
		{
			form = new Form(true) ; 	
			form.addEventListener(FieldErrorEvent.FIELD_ERROR, fieldErrorHandler) ; 
			
			form.messager.add("email_empty", "Your email is empty") ; 
			form.messager.add("email_invalid", "Your email is invalid") ; 
			form.messager.add("password_too_small", "Your password is too small") ; 
			form.messager.add("password_empty", "Your password is empty") ; 
			form.messager.add("password_confirm_empty", "You must confirm your password") ; 
			form.messager.add("password_confirm_too_small", "Your password confirm is too small") ; 
			form.messager.add("password_confirm_invalid", "Password and confirmation does not match") ; 						
			form.initFromMetadata(this) ; 			
			
			SimpleButton(view.getChildByName("btnOk")).addEventListener(MouseEvent.CLICK, validateForm ) ; 			
		}
		
		private function fieldErrorHandler(e:FieldErrorEvent):void 
		{
			// show the message in the error field : 
			view["tfError"].text = e.error.message ; 			
		}
		
		/**
		 * Add the form view to the scene.
		 * The view is designed in Flash IDE and not generated by the API
		 */
		private function buildView():void 
		{
			
			view = new FORM_VIEW() ; 
			view.x = view.y = 10 ; 
			addChild(view) ; 
			firstname = view["tfName"] ; 
			email = view["tfEmail"] ; 
			password = view["tfPassword"] ; 
			passwordConfirm = view["tfPasswordConfirm"] ; 
		}
		
		/**
		 * Validate form data 
		 * Error is displayed if necessary
		 * Values are taken
		 */
		private function validateForm(e:MouseEvent):void 
		{
			// validate the form values. The param tells how many errors you want on a validate ( 0 for all errors ) 
			var errors : Array = form.validate(1) ; 
			
			// no errors, form is ok
			if ( errors.length == 0 )
			{
				// get form values : 
				// if includeEmpty param is set to true, optional fields left blank will also be writen to the object
				var values:  Object = form.values() ; 
				
				// write result into error field
				view["tfError"].text = "Values OK :" ; 
				
				for ( var i : String in values ) 
				{
					view["tfError"].appendText( "\n"  + i + " : " + values[i] ) ; 
				}
			}
		}		
	}
}